<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;        //框架里面的总控制器的命名空间路径
use Common\Model\User;					//admin和home的公共控制器，Common/Model/下的User的命名空间
class UserController extends CommonController{         //会员管理控制器
	public function __init(){                          //构造函数，自动检测session是否存在判断是否登录
		if(!isset($_SESSION['username']) || !isset($_SESSION['uid'])){    //如果没有登录
			$str=<<<her
<script>
parent.location.href="./index.php?m=admin&c=Login";
</script>
her;
			echo $str;exit;
		}
	}
	public function changePwd(){	                   //修改密码
		if(IS_AJAX){                                   //如果有提交
			$password = Q('post.password');            //接受旧密码
			$model = new User;			               //实例化模型
			$password = $model->encrypt($_SESSION['username'],$password);     //把旧密码加密
			$data = $model->where("uid={$_SESSION['uid']}")->find();          //1.判断旧密码是否正确
			if($data['password'] != $password){
				echo json_encode(array('status'=>false,'message'=>'旧密码错误'));
				exit;
			}
			$newPassword = 	Q('post.newPassword');                          //2.判断两次密码是否相同
			if(empty($newPassword)){
				echo json_encode(array('status'=>false,'message'=>'新密码不能为空'));
				exit;
			}
			$confirmPassword = Q('post.confirmPassword');
			if(empty($confirmPassword)){
				echo json_encode(array('status'=>false,'message'=>'确认密码不能为空'));
				exit;
			}

			if($newPassword != $confirmPassword){
				echo json_encode(array('status'=>false,'message'=>'两次密码不一样'));
				exit;
			}
			$password = $model->encrypt($_SESSION['username'],$newPassword);  //3.修改密码
			$model->where("uid={$_SESSION['uid']}")->save(array('password'=>$password));  //把新密码存入数据库
			session_unset();                     //删除session变量
			session_destroy();					 //删除session文件
			echo json_encode(array('status'=>true,'message'=>'成功修改密码'));
				exit;	 //成功提示
		}
		View::make();
	}


}
?>